package com.kjj.car.controller;

import com.kjj.basic.util.LoginContext;
import com.kjj.car.domain.CarResources;
import com.kjj.car.dto.CarOnsaleOrOffsaleDto;
import com.kjj.car.es.doc.CarDoc;
import com.kjj.car.query.CarSearchQuery;
import com.kjj.car.service.ICarDetailService;
import com.kjj.car.service.ICarResourcesService;
import com.kjj.car.service.ICarSearchService;
import com.kjj.car.service.ICarService;
import com.kjj.car.domain.Car;
import com.kjj.car.query.CarQuery;
import com.kjj.basic.util.AjaxResult;
import com.kjj.basic.util.PageList;
import com.kjj.org.domain.Employee;
import com.kjj.org.domain.Shop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    public ICarService carService;

    @Autowired
    private ICarDetailService carDetailService;

    @Autowired
    private ICarResourcesService carResourcesService;


    /**
     * 保存和修改公用的
     *
     * @param car 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Car car) {
        //设置营销人员和店铺信息
        Employee seller = LoginContext.getLoginUser();
        car.setSellerId(seller.getId());
        Shop shop = seller.getShop();
        car.setShopId(shop.getId());
        car.setShopname(shop.getName());
        try {
            if (car.getId() != null)
                carService.update(car);
            else
                carService.add(car);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            //方案一 各自删 三条sql
/*            carService.del(id);
            carDetailService.delByCarId(id);
            carResourcesService.delByCarId(id);*/
            //方案二 连表删 一条sql
            carService.delByCarId(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id") Long id) {
        try {
            Car car = carService.getById(id);
            return AjaxResult.me().setData(car);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/resources/{id}")
    public AjaxResult getCarResourceById(@PathVariable("id") Long id) {
        try {
            CarResources carResources = carService.getResources(id);
            System.out.println(carResources);
            return AjaxResult.me().setData(carResources);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取车辆资源失败！" + e.getMessage());
        }
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping
    public AjaxResult list() {

        try {
            List<Car> list = carService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public AjaxResult json(@RequestBody CarQuery query, HttpServletRequest request) {
        String token = request.getHeader("token");
        System.out.println(token);
        try {
            PageList<Car> pageList = carService.queryPage(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }

    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids) {
        try {
            Employee loginUser = LoginContext.getLoginUser();
            carService.onsale(new CarOnsaleOrOffsaleDto(loginUser.getId(), loginUser.getUsername(), ids));
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上架失败！" + e.getMessage());
        }
    }

    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids) {
        try {
            Employee loginUser = LoginContext.getLoginUser();
            carService.offsale(new CarOnsaleOrOffsaleDto(loginUser.getId(), loginUser.getUsername(), ids));
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("下架失败！" + e.getMessage());
        }
    }

    @PutMapping("/reqcheck/{id}")
    public AjaxResult reqcheck(@PathVariable("id") Long id) {
        try {
            Car car = carService.getById(id);
            car.setAuditstate(0);
            carService.update(car);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("请求发送失败！" + e.getMessage());
        }
    }

    @PostMapping("/autodetect")
    public AjaxResult autodetect(@RequestBody Car car) {
        try {
            carService.autocheck(car);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("审核失败！可能有违规信息" + e.getMessage());
        }
    }

    @PutMapping("/pass")
    public AjaxResult pass(@RequestBody Car car) {
        try {
            carService.check(car);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("审核失败！" + e.getMessage());
        }
    }

    @PutMapping("/rejected")
    public AjaxResult rejected(@RequestBody Car car) {
        try {
            carService.check(car);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("审核失败！" + e.getMessage());
        }
    }

    //=============================================================//
    @Autowired
    private ICarSearchService carSearchService;
    @PostMapping("/list")
    public AjaxResult list(@RequestBody CarSearchQuery query){
        try {
            System.out.println(query);
            PageList<CarDoc> pageList = carSearchService.search(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("搜索失败！" + e.getMessage());
        }
    }
}
